using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;

namespace MCEMediaManager
{
    public class TorrentItem : ModelItem
    {
        #region Properties
        private string _Name;
        private long _Downloaded;
        private Uri _URL;
        public Uri URL
        {
            get { return this._URL; }
            set { this._URL = value; }
        }
        private string _FixedName;
        public string FixedName
        {
            get { return this._FixedName; }
            set { this._FixedName = value; }
        }
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (_Name != value)
                {
                    _Name = value;
                    _FixedName = TorrentItem.FixName(_Name);
                    FirePropertyChanged("Name");
                }
            }
        }
        public long Downloaded
        {
            get
            {
                return (this._Downloaded);
            }
            set
            {
                if (_Downloaded != value)
                {
                    this._Downloaded = value;
                    FirePropertyChanged("Downloaded");
                }
            }
        }
        private long _Size;
        public long Size
        {
            get
            {
                return (this._Size);
            }
            set
            {
                if (this._Size != value)
                {
                    this._Size = value;
                    FirePropertyChanged("Size");
                }
            }
        }
        public string DisplayName
        {
            get
            {
                return (this.FixedName + " " + Progress + "%");
           }
       }
        private int _progress;
        public int Progress
        {
            get { return this._progress; }
            set { this._progress = value; }
        }
        public Torrents Parent;
        #endregion

        #region Events
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                //You would do dispose stuff here.
            }
        }
        #endregion

        #region Methods
        public static string FixName(string sInput)
        {
            string sOrigInput = sInput;
            string sExt = "", sSeason = "00", sEpisode = "00", sOutput = "";
            int iEndOfName = 0;
            sExt = sInput.Substring(sInput.LastIndexOf(".") + 1).ToLower();
            if (sExt == "dvr-ms")
            {
                string sDate = sInput.Split('_')[4] + sInput.Split('_')[3] + sInput.Split('_')[2];
                sInput = sInput.Substring(0, sInput.IndexOf("_")) + " - " + sDate + ".dvr-ms";
                return sInput;
            }
            else if (sExt.Length > 6)
            {
                sExt = "";
            }

            sInput = sInput.ToUpper();
            sInput = sInput.Replace(" ", ".");
            sInput = sInput.Replace("-", ".");
            sInput = sInput.Replace("_", ".");

            string[] sTokens = sInput.Split('.');
            foreach (string sToken in sTokens)
            {
                iEndOfName = sInput.IndexOf(sToken) - 1;
                string sTokenFormat = sToken.Replace("0", "#").Replace("1", "#").Replace("2", "#").Replace("3", "#").Replace("4", "#").Replace("5", "#").Replace("6", "#").Replace("7", "#").Replace("8", "#").Replace("9", "#");
                if (sTokenFormat.Length <= 6 && sTokenFormat.StartsWith("S#") && sTokenFormat.Contains("E#"))
                {
                    sSeason = sToken.Substring(1, sToken.IndexOf("E") - 1);
                    sEpisode = sToken.Substring(sToken.IndexOf("E") + 1);
                    break;
                }
                if (sTokenFormat.Length <= 5 && sTokenFormat.Contains("#X#"))
                {
                    sSeason = sToken.Substring(0, sToken.IndexOf("X"));
                    sEpisode = sToken.Substring(sToken.IndexOf("X") + 1);
                    break;
                }
                if (sTokenFormat.Length >= 3 && sTokenFormat.Length <= 4 && sTokenFormat.Replace("#", "").Length == 0)
                {
                    if (sToken.Length == 3)
                    {
                        sSeason = sToken.Substring(0, 1);
                        sEpisode = sToken.Substring(1, 2);
                    }
                    if (sToken.Length == 4)
                    {
                        sSeason = sToken.Substring(0, 2);
                        sEpisode = sToken.Substring(2, 2);
                    }
                    break;
                }
            }

            try
            {
                if (sInput.StartsWith("THE."))
                    sOutput = TorrentItem.ProperCase(sInput.Substring(0, iEndOfName).Replace(".", " ")).Substring(4) + ", The - S" + sSeason.PadLeft(2, '0') + "E" + sEpisode.PadLeft(2, '0') + "." + sExt;
                else
                    sOutput = TorrentItem.ProperCase(sInput.Substring(0, iEndOfName).Replace(".", " ")) + " - S" + sSeason.PadLeft(2, '0') + "E" + sEpisode.PadLeft(2, '0') + "." + sExt;
                return sOutput;
            }
            catch
            {
                return sOrigInput;
            }
        }
        public static string ProperCase(string stringInput)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            bool fEmptyBefore = true;
            foreach (char ch in stringInput)
            {
                char chThis = ch;
                if (Char.IsWhiteSpace(chThis))
                    fEmptyBefore = true;
                else
                {
                    if (Char.IsLetter(chThis) && fEmptyBefore)
                        chThis = Char.ToUpper(chThis);
                    else
                        chThis = Char.ToLower(chThis);
                    fEmptyBefore = false;
                }
                sb.Append(chThis);
            }
            return sb.ToString();
        }
        //Send to uTorrent
        public bool Download()
        {
            bool value;
            if (Parent != null)
            {
                value = Parent.CurrentTorrentSession.AddTorrentFromURL(this.URL);
            }
            else
                value = false;
            if (value)
            {
                Application.Current.MessageBox(FixedName + " downloading.", "Torrent Added", 10, true);
                Parent.UpdateLists();
            }
            else
                Application.Current.MessageBox("There was an error downloading " + FixedName, "Torrent Error", 10, true);
            return value;
        }
#endregion
    }
}
